package com.utry.netty.client;


import android.util.Log;

import com.utry.netty.bean.PkgDataBean;
import com.utry.netty.util.JsonUitl;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.util.CharsetUtil;

public class JsonDecoder extends MessageToMessageDecoder<ByteBuf> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out)
            throws Exception {
        int byteNum = buf.readableBytes();
        byte[] bytes = new byte[byteNum];
        buf.readBytes(bytes);
        String dataStr = new String(bytes, CharsetUtil.UTF_8);
//        Log.e("NETTY", dataStr);
        Object user = JsonUitl.stringToObject(dataStr, Object.class);
        if (user != null)
            out.add(user);
    }
}
